const mongoose = require('mongoose');
const { User } = require('./db/user');

async function checkDatabase() {
  try {
    console.log('检查数据库连接和用户数据...\n');
    
    // 连接数据库
    await mongoose.connect('mongodb+srv://2334231668:xin123666@cluster0.13va5ah.mongodb.net/2410B');
    console.log('✅ 数据库连接成功');
    
    // 检查数据库名称
    console.log('当前数据库:', mongoose.connection.db.databaseName);
    
    // 列出所有集合
    const collections = await mongoose.connection.db.listCollections().toArray();
    console.log('\n数据库中的集合:');
    collections.forEach(col => {
      console.log(`- ${col.name}`);
    });
    
    // 检查users集合
    const userCount = await User.countDocuments();
    console.log(`\nusers集合中的用户数量: ${userCount}`);
    
    if (userCount > 0) {
      console.log('\n前5个用户:');
      const users = await User.find({}).limit(5);
      users.forEach((user, index) => {
        console.log(`${index + 1}. ID: ${user._id}`);
        console.log(`   用户名: ${user.username}`);
        console.log(`   头像: ${user.avatar}`);
        console.log(`   创建时间: ${user.createdAt}`);
        console.log('');
      });
    } else {
      console.log('\n❌ users集合为空，没有用户数据');
    }
    
    // 检查wechat_users集合
    const { WeChatUser } = require('./db/user');
    const wechatUserCount = await WeChatUser.countDocuments();
    console.log(`wechat_users集合中的用户数量: ${wechatUserCount}`);
    
    if (wechatUserCount > 0) {
      console.log('\n前3个微信用户:');
      const wechatUsers = await WeChatUser.find({}).limit(3);
      wechatUsers.forEach((user, index) => {
        console.log(`${index + 1}. ID: ${user._id}`);
        console.log(`   昵称: ${user.nickname}`);
        console.log(`   OpenID: ${user.openid}`);
        console.log('');
      });
    }
    
  } catch (error) {
    console.error('❌ 检查失败:', error.message);
  } finally {
    await mongoose.disconnect();
    console.log('\n数据库连接已关闭');
  }
}

// 运行检查
checkDatabase();
